﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SharpDropBoxClient.db;

//using SharpDropBoxClient.ResponseObjects;

namespace SharpDropBoxClient
{
    public class PolenterDbCache : IDbCache
    {
        #region ISterlingDbCache Members

        public MetaDataEntry getMetaDataEntry(string dbPath)
        {
            return DropBoxCacheDb.Instance.Data.Where(i=>i.Path.Equals(dbPath,StringComparison.InvariantCultureIgnoreCase )).FirstOrDefault();
        }

        public string getDbPathFromAreaAndPath(string area, string path)
        {
            string pathStartingWithSlash = "";
            if (!path.StartsWith("/") && !area.EndsWith(@"/"))
                pathStartingWithSlash = @"/" + path;
            else
                pathStartingWithSlash = path;
            return (area + pathStartingWithSlash).Replace(@"//", @"/");
        }

        public void SaveMetaDataEntry(MetaDataEntry saveEntry)
        {
            DropBoxCacheDb.Instance.Save(saveEntry);
            DropBoxCacheDb.Instance.Flush();
        }

        public void DeleteMetaDataEntry(MetaDataEntry entry)
        {
            DropBoxCacheDb.Instance.Delete(entry);
            DropBoxCacheDb.Instance.Flush();
        }

        //public SharpDropBoxClient.ResponseObjects.DirResult getDirResult(string dbPath)
        //{
        //    try
        //    {
        //        return DropBoxCacheDb.Instance.Load<SharpDropBoxClient.ResponseObjects.DirResult>(dbPath);
        //    }
        //    catch (SterlingException ex)
        //    {
        //        //Sterling DB is potentially corrupt.
        //        DropBoxCacheDb.Instance.Purge();
        //        return null;
        //    }
        //}

        //public void SaveDirResult(SharpDropBoxClient.ResponseObjects.DirResult entry)
        //{
        //    DropBoxCacheDb.Instance.Save<SharpDropBoxClient.ResponseObjects.DirResult>(entry);
        //    DropBoxCacheDb.Instance.Flush();
        //}

        //public void DeleteDirResult(SharpDropBoxClient.ResponseObjects.DirResult entry)
        //{
        //    DropBoxCacheDb.Instance.Delete<SharpDropBoxClient.ResponseObjects.DirResult>(entry);
        //    DropBoxCacheDb.Instance.Flush();
        //}

        #endregion ISterlingDbCache Members
    }

    public class NoDbCache : IDbCache
    {
        #region ISterlingDbCache Members

        public void DeleteMetaDataEntry(MetaDataEntry entry)
        {
        }

        public MetaDataEntry getMetaDataEntry(string dbPath)
        {
            return null;
        }

        public void SaveMetaDataEntry(MetaDataEntry entry)
        {
        }

        public string getDbPathFromAreaAndPath(string area, string path)
        {
            return (area + path).Replace(@"//", @"/");
        }

        #endregion ISterlingDbCache Members
    }
}